#ifndef EVENTSTRING_H
#define EVENTSTRING_H

#include <QVector>
#include <QDateTime>

class EventString
{
public:
    enum SECTION
    {
        NO_SECTION                  = 0x00000,
        AM_SECTION                  = 0x00001,
        PM_SECTION                  = 0x00002,
        MSECOND_SECTION             = 0x00004,
        SECOND_SECTION              = 0x00008,
        MINUTE_SECTION              = 0x00010,
        HOUR12_SECTION              = 0x00020,
        HOUR24_SECTION              = 0x00040,
        DAY_OF_MONTH_SECTION        = 0x00080,
        DAY_OF_YEAR_SECTION         = 0x00100,
        DAY_ORTHODOX_EASTER_SECTION = 0x00200,
        DAY_CATHOLIC_EASTER_SECTION = 0x00400,
        WEEK_SECTION                = 0x00800,
        FULL_WEEK_SECTION           = 0x01000,
        MONTH_SECTION               = 0x02000,
        YEAR2_SECTION               = 0x04000,
        YEAR4_SECTION               = 0x08000,
        YEAR2_INFO_SECTION          = 0x10000,
        YEAR4_INFO_SECTION          = 0x20000,
    };

    Q_DECLARE_FLAGS(SECTIONS, SECTION);

    struct SectionNode {
        SECTION type;
        int pos;
        int count;
        int order;
    };

    struct SectionGreater
    {
        bool operator() (const SectionNode &first, const SectionNode &second)
        {
            return (first.order > second.order);
        }
    };

    EventString();
    EventString(const QString &occur, const QString &format);
//    EventString(const EventString &event);
    const QString &occur() const { return m_occur; }
    const QString &format() const { return m_format; }
    const QDateTime &toDateTime();
    EventString &normalize();

    bool operator== (const EventString &event) const
    {
        return (occur() == event.occur()
                && format() == event.format());
    }

private:
    QString m_occur;
    QString m_format;
    QVector<SectionNode> m_sectionNodes;
    QDateTime m_dateTime;

    bool parseFormat(const QString &format);
    bool parseOccur(const QString &occur);
    int countRepeat(const QString &format, int index, int maxCount);
};

QDebug operator<< (QDebug, const EventString &);

#endif // EVENTSTRING_H
